import { Effect, ImmerReducer, Reducer, Subscription, request } from 'umi';
export interface IHostList {
  children?: IHostList[];
  content: string;
  createAt: string;
  email: string;
  hostId: string;
  html?: any;
  id: string;
  name: string;
  parentCommentId?: any;
  pass: boolean;
  replyUserEmail?: any;
  replyUserName?: any;
  updateAt: string;
  url: string;
  userAgent: string;
}
export interface IndexModelState {
  name: any;
  hostList: IHostList[],
  recommed:any[],
  size:number
}
export interface IParams {
  page: number,
  pageSize:number
}

export interface IndexModelType {
  namespace: 'about';
  state: IndexModelState;
  effects: {
    query: Effect;
    host: Effect;
    comment: Effect;
    recommed: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
    save1: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

function getData(){
  return fetch('https://api.blog.wipi.tech/api/page/about').then(res=>res.json());
}
function getHost(page:number,pagesize:number){
  return fetch(`https://api.blog.wipi.tech/api/comment/host/a5e81ffe-0ad0-4be9-acca-c0462b1b98a1?page=${page}&pageSize=${pagesize}`).then(res=>res.json());
}
function getRecommed(){
  return fetch(`https://api.blog.wipi.tech/api/article/recommend`).then(res=>res.json());
}
function Comment() {
  // console.log(data)
  return request(`https://api.blog.wipi.tech/api/comment`,{data:{content: 1,
  email: "376717307@qq.com",
  hostId: "a5e81ffe-0ad0-4be9-acca-c0462b1b98a1",
  name: "额",
    url: "/page/about"
  },
  method:"POST"}).then(res => res);
}

const IndexModels: IndexModelType = {
    //命名空间
  namespace: 'about',
    //redux 中存储的状态
  state: {
    name: {},
    hostList: [],
    recommed:[],
    size:0
  },
  //异步action
  effects: {
    *query({ payload }, { call, put, select }) {
      let state = yield select();
      // console.log('state...', state);
      let result = yield call(getData, 100, 200);
      // console.log(result)
      yield put({
        type: 'save1',
            payload: {
                name: result
            }
      })
    },
    *host({ payload }, { call, put, select }) {
      // let state = yield select();
      let result = yield call(getHost, payload.page, payload.pageSize);
      // console.log(result.data[0])
      yield put({
        type: 'save1',
            payload: {
              hostList: result.data[0],
              size:result.data[1]
            }
      })
    },
    *recommed({ payload }, { call, put, select }) {
      // let state = yield select();
      // console.log(payload)
      let result = yield call(getRecommed, payload,);
      console.log(result.data)
      yield put({
        type: 'save1',
            payload: {
              recommed: result.data,
            }
      })
    },
    *comment({ payload }, { call, put, select }) {
      // let state = yield select();
      // console.log(payload)
      let result = yield call(Comment, payload,);
      // console.log(result.data)
      yield put({
        type: 'save1',
            payload: {
              hostList: result.data[0],
              size:result.data[1]
            }
      })
    },
    
  },
  //同步action
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    save1(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default IndexModels;